import axios from 'axios'
// import { iconMap } from '@/../public/path2D/index.js'
// import { canvasDrawMap } from '@/../public/canvasDraw/index.js'
import { parseSvg } from '@meta2d/svg'

export async function getOtherIcons() {
  const result = []
  const datas = await Promise.all([
    // getUnicodeIcons(), // 引入字体图标
    getSvgs(),
    getPngs(),
    // getPath2Ds(),
    // getCanvasDraw()
  ])
  result.push(...datas.filter(Boolean))
  return result
}
function getUserDir(path: string, extend = []) {
  return async () => {
    const { data: fileList } = await axios.get(path)
    return fileList.concat(extend) // 合并路径，方便未来用户自定义扩充路径
  }
}
const userPensUrl = {
  icon: getUserDir('/icon/', []),
  svg: getUserDir('/svg/', []),
  png: getUserDir('/png/', []),
  path2D: getUserDir('/path2D/', []),
  canvasDraw: getUserDir('/canvasDraw/', [])
}
async function getUnicodeIcons() {
  let icons = []
  const iconsUrl = await userPensUrl.icon() // 获取字体图标的路径
  icons = await Promise.all(
    iconsUrl.map((url: { name: any }) => addIcons(url.name)) // 添加字体图标
  )
  return icons // 返回结果
}
async function addIcons(url: string) {
  // 请求路径
  const data = await fetch('/icon/' + url + '/iconfont.json').then((rsp) => rsp.json())
  const iconGroup = {
    name: data.name,
    loaded: true,
    show: true,
    list: [] as any[]
  }
  data.glyphs.map((item: any) =>
    iconGroup.list.push({
      // 侧边栏信息
      name: item.name,
      // 侧边栏目显示
      icon: data.css_prefix_text + item.font_class,
      data: {
        // 配置图元信息
        width: 100,
        height: 100,
        name: item.name, // 指定为icon类型
        iconFamily: data.font_family, // 字体图标配置
        icon: String.fromCharCode(item.unicode_decimal) // 字体图标
      }
    })
  )
  return iconGroup
}

async function getSvgs() {
  const folderName = '/svg/'
  const svgs = []
  const svgUrl = await userPensUrl.svg()
  for (const i of svgUrl) {
    if (i.type === 'directory') {
      const { data: files } = await axios.get(folderName + i.name + '/')
      svgs.push({
        name: i.name,
        count: files.length,
        list: [], // 进行懒加载 暂时不获取文件内容
        folder: true, // 标记为文件夹 懒加载时进行处理
        show: true,
        svg: true, // 标记为svg文件
        loaded: false // 是否已经懒加载
      })
    }
  }
  return svgs
}
async function getPngs() {
  const folderName = '/png/'
  const png = []
  const pngUrl = await userPensUrl.png()
  for (const i of pngUrl) {
    if (i.type === 'directory') {
      const { data: files } = await axios.get(folderName + i.name + '/')
      png.push({
        name: i.name,
        count: files.length,
        list: [], // 进行懒加载 暂时不获取文件内容
        folder: true, // 标记为文件夹 懒加载时进行处理
        show: true,
        svg: false, // 标记为png文件
        loaded: false
      })
    }
  }
  return png
}
async function getPath2Ds() {
  const folderName = '/path2D/'
  const path2d = []
  const path2DUrl = await userPensUrl.path2D()
  for (const i of path2DUrl) {
    if (i.type === 'directory') {
      const { data: files } = await axios.get(folderName + i.name + '/')
      const dataList = []
      for (const j of files) {
        const name = getFileName(j.name)
        dataList.push({
          name,
          icon: 'l-' + iconMap[name], // 侧边栏展示图标
          data: {
            width: 100,
            height: 100,
            name
            // text: name
          }
        })
      }
      path2d.push({
        name: i.name,
        count: files.length,
        list: dataList,
        show: true
      })
    }
  }
  return path2d
}
async function getCanvasDraw() {
  const folderName = '/canvasDraw/'
  const canvasDraw = []
  const canvasUrl = await userPensUrl.canvasDraw()
  for (const i of canvasUrl) {
    if (i.type === 'directory') {
      const { data: files } = await axios.get(folderName + i.name + '/')
      const dataList = []
      for (const j of files) {
        const name = getFileName(j.name)
        dataList.push({
          name,
          icon: 'l-' + canvasDrawMap[name], // 侧边栏展示图标
          data: {
            width: 100,
            height: 100,
            name,
            text: name
          }
        })
      }
      canvasDraw.push({
        name: i.name,
        count: files.length,
        list: dataList,
        show: true
      })
    }
  }
  return canvasDraw
}
function getFileName(name: string) {
  const n = name.lastIndexOf('.')
  if (n !== -1) {
    return name.substring(0, n)
  }
  return name
}
export async function svgToPens(f: any, dName: any) {
  const name = getFileName(f.name)
  const image = '/svg/' + dName + '/' + f.name
  const pens = parseSvg(await fetch(image).then((res) => res.text()))
  pens[0].name = name
  return {
    name,
    image,
    data: pens,
    component: true
  }
}
export async function pngToPens(f: any, dName: any) {
  const name = getFileName(f.name)
  const image = '/png/' + dName + '/' + f.name
  return {
    name,
    image
  }
}
